<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>在线考试系统 - 创建考试</title>
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/css/all.min.css">
    <style>
        :root {
            --primary: #4361ee;
            --secondary: #3f37c9;
            --teacher-accent: #4361ee;
            --card-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            --transition: all 0.3s ease;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f7fb;
            color: #333;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .navbar {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 0.8rem 1rem;
        }

        .navbar-brand {
            font-weight: 700;
            font-size: 1.5rem;
            display: flex;
            align-items: center;
        }

        .navbar-brand i {
            margin-right: 10px;
            font-size: 1.8rem;
        }

        .nav-link {
            color: rgba(255, 255, 255, 0.85) !important;
            font-weight: 500;
            padding: 0.5rem 1rem !important;
            border-radius: 4px;
            margin: 0 3px;
            transition: var(--transition);
        }

        .nav-link:hover, .nav-link.active {
            background-color: rgba(255, 255, 255, 0.15);
            color: white !important;
        }

        .main-content {
            flex: 1;
            padding: 2rem 0;
        }

        .page-header {
            background: white;
            border-radius: 15px;
            padding: 1.5rem;
            box-shadow: var(--card-shadow);
            margin-bottom: 2rem;
            position: relative;
            overflow: hidden;
        }

        .page-header::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 5px;
            height: 100%;
            background-color: var(--teacher-accent);
        }

        .form-card {
            background: white;
            border-radius: 12px;
            box-shadow: var(--card-shadow);
            padding: 2rem;
            margin-bottom: 2rem;
        }

        .form-label {
            font-weight: 600;
            color: #2c3e50;
        }

        .form-control {
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            padding: 0.75rem 1rem;
            transition: var(--transition);
        }

        .form-control:focus {
            border-color: var(--teacher-accent);
            box-shadow: 0 0 0 0.2rem rgba(67, 97, 238, 0.25);
        }

        .btn-primary {
            background: linear-gradient(135deg, var(--teacher-accent), #3a0ca3);
            border: none;
            padding: 0.75rem 2rem;
            font-weight: 600;
            border-radius: 8px;
            transition: var(--transition);
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(67, 97, 238, 0.4);
        }

        .question-section {
            background: #f8f9fa;
            border-radius: 8px;
            padding: 1.5rem;
            margin-bottom: 1.5rem;
        }

        .question-list {
            max-height: 400px;
            overflow-y: auto;
            padding: 1rem;
            background: white;
            border-radius: 8px;
            border: 1px solid #e0e0e0;
        }

        .question-item {
            padding: 1rem;
            border-bottom: 1px solid #e0e0e0;
            transition: var(--transition);
        }

        .question-item:hover {
            background-color: #f8f9fa;
        }

        .question-item:last-child {
            border-bottom: none;
        }

        .total-score {
            font-size: 1.25rem;
            font-weight: 600;
            color: var(--teacher-accent);
            padding: 1rem;
            background: #e3f2fd;
            border-radius: 8px;
            margin-top: 1rem;
        }

        .footer {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            color: white;
            padding: 2rem 0;
            margin-top: auto;
        }
    </style>
</head>
<body>
<!-- 导航栏 -->
<nav class="navbar navbar-expand-lg navbar-dark">
    <div class="container">
        <a class="navbar-brand" href="/teacher/home">
            <i class="fas fa-chalkboard-teacher"></i>在线考试系统
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav me-auto">
                <li class="nav-item">
                    <a class="nav-link" href="/home/manage"><i class="fas fa-home"></i> 主页</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link active" href="/exam/manage"><i class="fas fa-tasks"></i> 考试管理</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/teacher/question/manage"><i class="fas fa-book"></i> 题库管理</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/teacher/score/analysis"><i class="fas fa-chart-bar"></i> 成绩分析</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

<!-- 主内容区域 -->
<div class="main-content">
    <div class="container">
        <div class="page-header">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <h2 class="mb-0">创建新考试</h2>
                    <p class="text-muted mb-0">填写考试信息并选择试题</p>
                </div>
                <a href="/exam/manage" class="btn btn-outline-primary">
                    <i class="fas fa-arrow-left me-2"></i>返回考试列表
                </a>
            </div>
        </div>

        <form th:action="@{/exam/create}" method="post" id="examForm">
            <!-- 基本信息 -->
            <div class="form-card">
                <h4 class="mb-4">基本信息</h4>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">考试名称</label>
                        <input type="text" class="form-control" name="examName" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">考试类型</label>
                        <select class="form-select" name="examType" required>
                            <option value="期中">期中考试</option>
                            <option value="期末">期末考试</option>
                            <option value="模拟">模拟考试</option>
                            <option value="测验">课堂测验</option>
                        </select>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">开始时间</label>
                        <input type="datetime-local" class="form-control" name="startTime" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">结束时间</label>
                        <input type="datetime-local" class="form-control" name="endTime" required>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">考试时长（分钟）</label>
                        <input type="number" class="form-control" name="duration" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label class="form-label">考试课程</label>
                        <select class="form-select" name="courseId" id="courseSelect" required>
                            <option value="">请选择课程</option>
                        </select>
                    </div>
                </div>
            </div>

            <!-- 选择班级 -->
            <div class="form-card">
                <h4 class="mb-4">选择考试班级</h4>
                <div class="row" id="classList">
                </div>
            </div>

            <!-- 选择试题 -->
            <div class="form-card">
                <h4 class="mb-4">选择试题</h4>
                <div class="question-section">
                    <div class="mb-3">
                        <label class="form-label">试题类型</label>
                        <div class="btn-group" role="group">
                            <input type="radio" class="btn-check" name="questionType" id="all" value="all" checked>
                            <label class="btn btn-outline-primary" for="all">全部</label>
                            <input type="radio" class="btn-check" name="questionType" id="choice" value="choice">
                            <label class="btn btn-outline-primary" for="choice">选择题</label>
                            <input type="radio" class="btn-check" name="questionType" id="fill" value="fill">
                            <label class="btn btn-outline-primary" for="fill">填空题</label>
                            <input type="radio" class="btn-check" name="questionType" id="essay" value="essay">
                            <label class="btn btn-outline-primary" for="essay">简答题</label>
                        </div>
                    </div>
                    <div class="question-list" id="questionList">
                    </div>
                    <div class="total-score">
                        总分：<span id="totalScore">0</span> 分
                    </div>
                </div>
            </div>

            <div class="text-center mt-4">
                <button type="submit" class="btn btn-primary btn-lg">
                    <i class="fas fa-save me-2"></i>创建考试
                </button>
            </div>
        </form>
    </div>
</div>

<!-- 页脚 -->
<footer class="footer">
    <div class="container text-center py-4">
        <a href="https://gitee.com/sorrymaker04/onlineexam-workload" target="_blank"><i class="fab fa-github fa-2x"></i></a>
        <div class="copyright mt-2">
            &copy; 2025 版权所有 北城在线考试系统小组 12 Team
        </div>
    </div>
</footer>

<!-- Bootstrap JS -->
<script src="/js/bootstrap.bundle.min.js"></script>

<!-- 自定义JS -->
<script>
// 添加一个全局变量来存储已选择的试题
let selectedQuestions = new Set();

document.addEventListener('DOMContentLoaded', function() {
    // 加载教师课程
    loadTeacherCourses();
    
    // 加载班级列表
    loadClasses();
    
    // 监听课程选择变化
    document.getElementById('courseSelect').addEventListener('change', function() {
        const courseId = this.value;
        if (courseId) {
            loadQuestions(courseId);
        }
    });
    
    // 监听试题类型选择
    document.querySelectorAll('input[name="questionType"]').forEach(radio => {
        radio.addEventListener('change', function() {
            const courseId = document.getElementById('courseSelect').value;
            if (courseId) {
                loadQuestions(courseId);
            }
        });
    });
});

// 加载教师课程
function loadTeacherCourses() {
    fetch('/exam/api/teacher/courses')
        .then(response => response.json())
        .then(courses => {
            const select = document.getElementById('courseSelect');
            select.innerHTML = '<option value="">请选择课程</option>';
            courses.forEach(course => {
                const option = document.createElement('option');
                option.value = course.courseId;
                option.textContent = course.courseName;
                select.appendChild(option);
            });
        })
        .catch(error => console.error('加载课程失败:', error));
}

// 加载班级列表
function loadClasses() {
    fetch('/exam/api/teacher/classes')
        .then(response => response.json())
        .then(classes => {
            const container = document.getElementById('classList');
            container.innerHTML = '';
            classes.forEach(cls => {
                const col = document.createElement('div');
                col.className = 'col-md-4 mb-3';
                col.innerHTML = `
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" 
                               name="classIds" value="${cls.classId}" 
                               id="class${cls.classId}">
                        <label class="form-check-label" for="class${cls.classId}">
                            ${cls.className}
                        </label>
                    </div>
                `;
                container.appendChild(col);
            });
        })
        .catch(error => console.error('加载班级失败:', error));
}

// 加载试题
function loadQuestions(courseId) {
    const type = document.querySelector('input[name="questionType"]:checked').value;
    fetch(`/exam/api/questions?courseId=${courseId}&type=${type}`)
        .then(response => response.json())
        .then(questions => {
            const container = document.getElementById('questionList');
            container.innerHTML = '';
            questions.forEach(question => {
                const div = document.createElement('div');
                div.className = 'question-item';
                const isChecked = selectedQuestions.has(question.questionId.toString());
                div.innerHTML = `
                    <div class="form-check">
                        <input class="form-check-input question-checkbox" type="checkbox" 
                               name="questionIds" value="${question.questionId}" 
                               data-score="${question.score}"
                               ${isChecked ? 'checked' : ''}
                               onchange="handleQuestionSelection(this)">
                        <label class="form-check-label">
                            <strong>${question.type === 'choice' ? '选择题' : 
                                     question.type === 'fill' ? '填空题' : '简答题'}</strong>
                            <span class="ms-2">${question.content}</span>
                            <span class="float-end">${question.score}分</span>
                        </label>
                    </div>
                `;
                container.appendChild(div);
            });
            updateTotalScore();
        })
        .catch(error => console.error('加载试题失败:', error));
}

// 处理试题选择
function handleQuestionSelection(checkbox) {
    const questionId = checkbox.value;
    if (checkbox.checked) {
        selectedQuestions.add(questionId);
    } else {
        selectedQuestions.delete(questionId);
    }
    updateTotalScore();
}

// 更新总分
function updateTotalScore() {
    const checkboxes = document.querySelectorAll('.question-checkbox:checked');
    let total = 0;
    checkboxes.forEach(checkbox => {
        total += parseFloat(checkbox.dataset.score);
    });
    document.getElementById('totalScore').textContent = total.toFixed(1);
}

// 表单提交前验证
document.getElementById('examForm').addEventListener('submit', function(e) {
    const selectedQuestions = document.querySelectorAll('input[name="questionIds"]:checked');
    const selectedClasses = document.querySelectorAll('input[name="classIds"]:checked');
    
    if (selectedQuestions.length === 0) {
        e.preventDefault();
        alert('请至少选择一道试题');
        return;
    }
    
    if (selectedClasses.length === 0) {
        e.preventDefault();
        alert('请至少选择一个班级');
        return;
    }

    // 计算总分
    let totalScore = 0;
    selectedQuestions.forEach(checkbox => {
        totalScore += parseFloat(checkbox.dataset.score);
    });

    // 创建隐藏的input字段来存储总分
    const totalScoreInput = document.createElement('input');
    totalScoreInput.type = 'hidden';
    totalScoreInput.name = 'totalScore';
    totalScoreInput.value = totalScore.toFixed(1);
    this.appendChild(totalScoreInput);
});
</script>
</body>
</html> 